package longrise.phone.com.bjjt_henan.compensate;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.ui.AbstractSpinerAdapter.IOnItemSelectListener;
import com.longrise.bjjt.util.AllCapTransformationMethod;
import com.longrise.bjjt.util.FileUtils;
import com.longrise.bjjt.util.StringUtils;
import com.longrise.bjjt.util.UiUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CheckCard;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.Utils.PermissionHelper;
import longrise.phone.com.bjjt_henan.bindinginformation.BindCarView;

/**
 * 事故上传的信息填写界面，Created by Administrator on 2016/5/12.
 */
public class AccidentLoadingView extends LFView implements View.OnClickListener, IOnItemSelectListener, ILSMsgListener
        , ILFMsgListener, MainActivity.OnActivityReturnBitmap {
    private String appCaseNo;//案件编号
    private Context context = null;
    private View view = null;
    private Button bt_accidentloading;
    private EditText et_accidentloadingpic1;
    private EditText et_accidentloadingpic2;
    private EditText et_accidentloadingpic3;
    private TextView et_accidentloadingpic4;
    private EditText et_accidentloadingpic5;
    private Button bt_accidentloading_record;
    private BaseApplication mApplication;
    private Dialog processDialog;
    private EntityBean[] DrivingLicenceBeans;//车辆信息
    private EntityBean myDrivingLiscenceBean;//驾驶证的bean
    private List<String> carnoList = null;
    private RelativeLayout rl_accidentloading;
    private RadioGroup rg_accident1;
    private String dfCarType;
    private RadioButton rb_accident_1;
    private RadioButton rb_accident_2;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private String accidentType;
    private String discription;
    private EditText et_accdentidcardhe;
    private EditText et_accdentidcardme;
    private EditText framnoET;//车架号

    private EditText otherInsnoET;//对方保单号

    private TextView otherGetTV;//获取对方保单号

    private EditText myInsnoET;//己方保单号

    private TextView myGetTV;//获取己方保单号

    private Dialog cpsDialog;

    private ImageView cameraIV1;

    private ImageView cameraIV2;

    private String type;

    private LinearLayout carnumLLayout;

    private TextView carnumTV;

    private List<String> carnoStrs;

    public AccidentLoadingView(Context context, String appCaseNo, EntityBean myDrivingLiscenceBean, String accidentType, String discription) {
        super(context);
        this.context = context;
        this.appCaseNo = appCaseNo;
        this.mApplication = ((MainActivity) context).getAppData();
        this.myDrivingLiscenceBean = myDrivingLiscenceBean;
        this.accidentType = accidentType;
        this.discription = discription;

        if (context instanceof MainActivity) {
            ((MainActivity) context).setOnActivityReturnBitmap(this);
        }
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {
        checkBindingCarInfo();
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        initData();
    }

    //查询
    private void initData() {
        carnoList = new ArrayList<String>();
    }


    private void initView() {
        view = View.inflate(context, R.layout.view_accidentloading, null);
        bt_accidentloading = (Button) view.findViewById(R.id.bt_accidentloading);
        rg_accident1 = (RadioGroup) view.findViewById(R.id.rg_accident1);
        rb_accident_1 = (RadioButton) view.findViewById(R.id.rb_accident_1);
        rb_accident_2 = (RadioButton) view.findViewById(R.id.rb_accident_2);
        bt_accidentloading_record = (Button) view.findViewById(R.id.bt_accidentloading_record);
        et_accidentloadingpic1 = (EditText) view.findViewById(R.id.et_accidentloadingpic1);
        et_accidentloadingpic2 = (EditText) view.findViewById(R.id.et_accidentloadingpic2);
        et_accidentloadingpic3 = (EditText) view.findViewById(R.id.et_accidentloadingpic3);
        et_accidentloadingpic4 = (TextView) view.findViewById(R.id.et_accidentloadingpic4);
        et_accidentloadingpic5 = (EditText) view.findViewById(R.id.et_accidentloadingpic5);
        et_accdentidcardhe = (EditText) view.findViewById(R.id.et_accdentidcardhe);//对方驾驶证号
        et_accdentidcardme = (EditText) view.findViewById(R.id.et_accdentidcardme);//本方驾驶证号
        et_accdentidcardme.setText(myDrivingLiscenceBean.getString("cardno"));
        et_accidentloadingpic5.setText(myDrivingLiscenceBean.getString("carownername"));
        rl_accidentloading = (RelativeLayout) view.findViewById(R.id.rl_accidentloading);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("事故上传");

        framnoET = (EditText) view.findViewById(R.id.et_frameno);
        carnumLLayout = (LinearLayout) view.findViewById(R.id.load_ll_carnum);
        carnumTV = (TextView) view.findViewById(R.id.load_tv_carnum);
        //自动转换为大写
        framnoET.setTransformationMethod(new AllCapTransformationMethod());
        et_accidentloadingpic2.setTransformationMethod(new AllCapTransformationMethod());
        et_accdentidcardhe.setTransformationMethod(new AllCapTransformationMethod());
        et_accdentidcardme.setTransformationMethod(new AllCapTransformationMethod());
        et_accidentloadingpic2.setFilters(new InputFilter[]{new MaxLengthFilter(6, context)});

        otherInsnoET = (EditText) view.findViewById(R.id.aload_et_otherinsno);
        otherGetTV = (TextView) view.findViewById(R.id.aload_tv_otherget);
        myInsnoET = (EditText) view.findViewById(R.id.aload_et_myinsno);
        myGetTV = (TextView) view.findViewById(R.id.aload_tv_myget);

        cameraIV1 = (ImageView) view.findViewById(R.id.iv_other_camera1);
        cameraIV2 = (ImageView) view.findViewById(R.id.iv_other_camera2);

        //统计区分
        String text = context.getResources().getString(R.string.accident_commit);
        bt_accidentloading.setText(CommontUtils.changeStrColor(text, "提交", Color.WHITE));
        carnoStrs = CommontUtils.getCarnoShorts();

        ((MainActivity) context).initHelpView(view, appCaseNo);
    }

    private void regEvent(boolean b) {
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (null != bt_accidentloading) {
            bt_accidentloading.setOnClickListener(b ? this : null);
        }
        if (null != bt_accidentloading_record) {
            bt_accidentloading_record.setOnClickListener(b ? this : null);
        }
        if (null != rl_accidentloading) {
            rl_accidentloading.setOnClickListener(b ? this : null);
        }
        if (null != rg_accident1) {
            rg_accident1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_accident_1:
                            dfCarType = "1";
                            break;
                        case R.id.rb_accident_2:
                            dfCarType = "2";
                            break;
                    }
                }
            });
        }
        if (b) {
            this.addILSMsgListener(this);
            this.addILFMsgListener(this);
        } else {
            this.removeILSMsgListener(this);
            this.removeILFMsgListener(this);
        }
        if (null != carnumLLayout) {
            carnumLLayout.setOnClickListener(b ? this : null);
        }
        if (null != otherGetTV) {
            otherGetTV.setOnClickListener(b ? this : null);
        }
        if (null != myGetTV) {
            myGetTV.setOnClickListener(b ? this : null);
        }
        if (null != cameraIV1) {
            cameraIV1.setOnClickListener(b ? this : null);
        }
        if (null != cameraIV2) {
            cameraIV2.setOnClickListener(b ? this : null);
        }
    }

    //增加输入长度的限制提示
    public static class MaxLengthFilter implements InputFilter {
        private final int mMax;
        private Context context;

        public MaxLengthFilter(int max, Context context) {
            mMax = max;
            this.context = context;
        }

        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                   int dstart, int dend) {
            int keep = mMax - (dest.length() - (dend - dstart));
            if (keep < (end - start)) {
                UiUtil.showToast(context, "最多只能为6位！");
            }
            if (keep <= 0) {
                return "";
            } else if (keep >= end - start) {
                return null; // keep original
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        }

        /**
         * @return the maximum length enforced by this input filter
         */
        public int getMax() {
            return mMax;
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.hn_btn_back:
                    OnDestroy();
                    closeForm();
                    break;
                case R.id.bt_accidentloading_record:
                    BindCarView bindcarview = new BindCarView(context, null);
                    FrameworkManager.getInstance().showNewForm(context, bindcarview);
                    break;
                case R.id.rl_accidentloading:
                    UiUtil.showSelectPopWindow(context, carnoList, this, rl_accidentloading, true);
                    break;
                case R.id.bt_accidentloading:
                    if (!ifEdittextNull()) {
                        return;
                    }
                    //提交
                    submit();
                    break;
                case R.id.aload_tv_myget://获取己方保单
                    String mycarno = et_accidentloadingpic4.getText().toString().trim();
                    if (TextUtils.isEmpty(mycarno)) {
                        UiUtil.showToast(context, "请选择本方车牌号");
                        return;
                    }
                    String myframeno = getMyFrameno();
                    getCpsno(mycarno, myframeno);
                    myInsnoET.setText("");//重置保单号
                    break;
                case R.id.aload_tv_otherget://获取对方保单
                    if (!checkOtherCarno()) {
                        return;
                    }
                    String otherCarno = et_accidentloadingpic2.getText().toString().trim().toUpperCase();
                    otherCarno = carnumTV.getText().toString() + otherCarno;
                    if (!checkOtherFrameno()) {
                        return;
                    }
                    String otherFrameno = framnoET.getText().toString().trim().toUpperCase();
                    getCpsno(otherCarno, otherFrameno);
                    otherInsnoET.setText("");//重置保单号
                    break;
                case R.id.iv_other_camera1:
                    freshCamera("5");
                    break;
                case R.id.iv_other_camera2:
                    freshCamera("6");
                    break;
                case R.id.load_ll_carnum://车牌
                    UiUtil.showSelectPopWindow(context, carnoStrs, this, carnumLLayout, true);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void freshCamera(String code) {
        if (!PermissionHelper.checkCameraPermissions(context)) {
            return;
        }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        if (TextUtils.equals("6", code)) {
            bundle.putInt("backGroundResourceId", R.mipmap.xsz_5);
        } else if (TextUtils.equals("5", code)) {
            bundle.putInt("backGroundResourceId", R.mipmap.jsz_4);
        }
        intent.putExtras(bundle);
        type = code;
        intent.setClass(context, HeNanSurfaceViewCameraActivity.class);
        ((Activity) context).startActivityForResult(intent, 96);
    }

    @Override
    public void activityReturnBitmap(int requestCode, int resultCode, Intent data) {
        if (requestCode == 96 && resultCode == HeNanSurfaceViewCameraActivity.RESULT_OK) {
            final String path = data.getStringExtra("image");
            if (TextUtils.isEmpty(path)) {
                UiUtil.showToast(context, "拍照图片有误，请重新拍照！");
                return;
            }
            String imageStr = FileUtils.File2byte(new File(path), true);
            processDialog = UiUtil.showProcessDialog(context, "正在识别,请稍后...");
            ApiImpI.getInstance(context.getApplicationContext()).getOcrInfo(imageStr, type, new ApiImpI.RequestCallBack() {
                @Override
                public void onError(String error) {
                    if (null != processDialog && processDialog.isShowing()) {
                        processDialog.dismiss();
                    }
                    UiUtil.showToast(context, "图像识别失败，请重新拍照或者手动填写");
                }

                @Override
                public void onSuccess(Object result) {
                    try {
                        if (null != processDialog && processDialog.isShowing()) {
                            processDialog.dismiss();
                        }
                        if (view == null) {
                            return;
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if ("1".equals(restate)) {
                            //图像识别成功，将内容填充到界面
                            EntityBean dataBean = bean.getBean("data");
                            if (null != dataBean) {
                                if (TextUtils.equals("6", type)) {
                                    String carno = dataBean.getString("carno");//车牌号
                                    String vin = dataBean.getString("vin");//车架号
                                    if (StringUtils.isCar(carno)) {
                                        int pos = carnoStrs.indexOf(carno.substring(0, 1));//截取表示区域的首汉字
                                        pos = (pos == -1) ? 0 : pos;
                                        carnumTV.setText(carnoStrs.get(pos));

                                        String end = carno.substring(1);
                                        et_accidentloadingpic2.setText(end);
                                    }
                                    if (CommontUtils.isCarvin(vin)) {
                                        framnoET.setText(vin);
                                    }
                                } else if (TextUtils.equals("5", type)) {
                                    String name = dataBean.getString("name");//姓名
                                    String idCartd = dataBean.getString("cardno");//身份证号
                                    if (CommontUtils.isChinaName(name)) {
                                        et_accidentloadingpic1.setText(name);
                                    }
                                    if (CheckCard.IDCardValidate(idCartd)) {
                                        et_accdentidcardhe.setText(idCartd);
                                    }
                                }
                                return;
                            }
                        }
                        String redes = bean.getString("redes");
                        UiUtil.showToast(context, redes);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    //验证对方输入的车牌号
    private boolean checkOtherCarno() {
        String otherCarno = et_accidentloadingpic2.getText().toString().trim().toUpperCase();
        otherCarno = carnumTV.getText().toString() + otherCarno;
        if (!StringUtils.isCar(otherCarno)) {
            UiUtil.showToast(context, "对方的车牌号格式不正确");
            return false;
        }
        String myCarno = et_accidentloadingpic4.getText().toString().trim();
        if (TextUtils.equals(otherCarno, myCarno)) {
            UiUtil.showToast(context, "对方的车牌号不能同己方一致");
            return false;
        }
        return true;
    }

    //验证对方输入的车架号
    private boolean checkOtherFrameno() {
        String vin = framnoET.getText().toString().trim().toUpperCase();
        if (!CommontUtils.isCarvin(vin)) {
            UiUtil.showToast(context, "请输入正确的车架号");
            return false;
        }
        if (TextUtils.equals(vin, getMyFrameno())) {
            UiUtil.showToast(context, "对方的车架号不能同己方一致");
            return false;
        }
        return true;
    }

    //获取保单号
    private void getCpsno(String carno, String frameno) {
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
//        DialogUtils.showDialog(context);
        if (null == cpsDialog) {
            cpsDialog = UiUtil.showProcessDialog(context, "查询承保信息中");
        } else {
            cpsDialog.show();
        }
        ApiImpI.getInstance(context).searchCpsNum(uid, token, carno, frameno, appCaseNo, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
//                DialogUtils.closeDialog();
                if (null != cpsDialog && cpsDialog.isShowing()) {
                    cpsDialog.dismiss();
                }
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(context, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
//                DialogUtils.closeDialog();
                if (null != cpsDialog && cpsDialog.isShowing()) {
                    cpsDialog.dismiss();
                }
                if (null == view) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    String isValid = dataBean.getString("isVaild");
                    if (TextUtils.equals(isValid, "0")) {
                        String carno = dataBean.getString("casecarno");
                        String insno = dataBean.getString("trafficinsno");
                        String mycarno = et_accidentloadingpic4.getText().toString().trim();
                        if (TextUtils.equals(carno, mycarno)) {//获取的是己方保单
                            myInsnoET.setText(insno);
                        } else {
                            otherInsnoET.setText(insno);
                        }
                    }
                } else {
                    String error = bean.getString("redes");
                    if (!TextUtils.isEmpty(error)) {
                        UiUtil.showToast(context, error);
                    }
                }
            }
        });
    }

    //获取本方选中车辆的车架号
    private String getMyFrameno() {
        String mycarno = et_accidentloadingpic4.getText().toString().trim();
        for (EntityBean bean : DrivingLicenceBeans) {
            String getCarInfo = bean.getString("carno");
            if (mycarno.equals(getCarInfo)) {
                return bean.getString("identificationnum");//车架号
            }
        }
        return null;
    }

    //创造双方的bean跳转,这是责任认定书的bean[]
    private void submit() {
        EntityBean hisBean = new EntityBean();
        hisBean.set("carType", dfCarType);
        hisBean.set("carownname", et_accidentloadingpic1.getText().toString().trim());
        String selectShort = carnumTV.getText().toString();
        hisBean.set("casecarno", selectShort + et_accidentloadingpic2.getText().toString().trim().toUpperCase());
        hisBean.set("casecarphone", et_accidentloadingpic3.getText().toString().trim());
        hisBean.set("casecarId", et_accdentidcardhe.getText().toString().trim().toUpperCase());
        hisBean.set("frameno", framnoET.getText().toString().trim().toUpperCase());
        hisBean.set("trafficinsno", otherInsnoET.getText().toString().trim());

        EntityBean myBean = new EntityBean();
        myBean.set("carownname", et_accidentloadingpic5.getText().toString().trim());
        myBean.set("casecarno", et_accidentloadingpic4.getText().toString().trim());//车牌号
        myBean.set("casecarId", et_accdentidcardme.getText().toString().trim().toUpperCase());//驾驶证号
        myBean.set("accidentType", accidentType);
        myBean.set("discription", discription);
        myBean.set("appCaseNo", appCaseNo);
        myBean.set("frameno", getMyFrameno());
        myBean.set("trafficinsno", myInsnoET.getText().toString().trim());
        EntityBean[] disputeBean = new EntityBean[]{hisBean, myBean};

        checkCarsValid(disputeBean);
    }

    //验证对方车辆的行驶证
    private void checkCarsValid(final EntityBean[] carlist) {
        String userflag = mApplication.getKckpName();
        String token = mApplication.getToken();
        String othercarno = carlist[0].getString("casecarno");
        String otherframeno = carlist[0].getString("frameno");
        String name = carlist[0].getString("carownname");
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context.getApplicationContext()).checkDriving(userflag, token, othercarno, otherframeno, name,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(context, error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        if (null == view) {
                            return;
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if (TextUtils.equals(restate, "1")) {
                            DisputeView disputeview = new DisputeView(context, myDrivingLiscenceBean, DrivingLicenceBeans, carlist);
                            FrameworkManager.getInstance().showNewForm(context, disputeview);
                        } else {
                            UiUtil.showToast(context, redes);
                        }
                    }
                });
    }

    /**
     * 判断输入框是否为空
     *
     * @return
     */
    private boolean ifEdittextNull() throws Exception {
        if (null == dfCarType) {
            UiUtil.showToast(context, "请选择车辆类型");
            return false;
        }
        String et_accidentloadingpic1content = et_accidentloadingpic1.getText().toString().trim();
        if (TextUtils.isEmpty(et_accidentloadingpic1content)) {
            UiUtil.showToast(context, "姓名为空");
            return false;
        }
        //对方车牌号
        if (!checkOtherCarno()) {
            return false;
        }
        //对方车架号
        if (!checkOtherFrameno()) {
            return false;
        }
        EntityBean userinfo = mApplication.getLoginInfo().getBean("userinfo");
        String phoneNumber = userinfo.getString("mobilephone");
        String phoneHis = et_accidentloadingpic3.getText().toString().trim();
        //判断手机号是否正确
        if (!StringUtils.isPhone(phoneHis)) {
            UiUtil.showToast(context, "请输入正确的手机号");
            return false;
        }
        //判断双方的手机号是否相同
        if (phoneHis.equals(phoneNumber)) {
            UiUtil.showToast(context, "对方的手机号不能同己方一致");
            return false;
        }
        String cardno = et_accdentidcardhe.getText().toString().trim().toUpperCase();
        if (!CheckCard.IDCardValidate(cardno)) {
            UiUtil.showToast(context, "对方驾驶证号输入不正确");
            return false;
        }
        String mycardno = et_accdentidcardme.getText().toString().trim().toUpperCase();
        if (TextUtils.equals(mycardno, cardno)) {
            UiUtil.showToast(context, "对方的驾驶证号不能同己方一致");
            return false;
        }
        String et_accidentloadingpic4content = et_accidentloadingpic4.getText().toString().trim();
        if (TextUtils.isEmpty(et_accidentloadingpic4content)) {
            UiUtil.showToast(context, "己方车牌号为空");
            return false;
        }
        String et_accidentloadingpic5content = et_accidentloadingpic5.getText().toString().trim();
        if (TextUtils.isEmpty(et_accidentloadingpic5content)) {
            UiUtil.showToast(context, "当事人为空");
            return false;
        }
        if (TextUtils.isEmpty(mycardno)) {
            UiUtil.showToast(context, "己方驾驶证号为空");
            return false;
        }
        if (TextUtils.equals(mycardno, cardno)) {
            UiUtil.showToast(context, "对方的驾驶证号不能同己方一致");
            return false;
        }
        return true;
    }

    @Override
    public void onItemClick(int pos, View view) {

        switch (view.getId()) {
            case R.id.rl_accidentloading:
                String current = et_accidentloadingpic4.getText().toString().trim();
                String select = carnoList.get(pos);
                if (TextUtils.equals(current, select)) {//选择的同一个
                    return;
                }
                et_accidentloadingpic4.setText(carnoList.get(pos));
                //cartypeString = carTypeList.get(pos);
                myInsnoET.setText("");
                break;
            case R.id.load_ll_carnum://车牌
                carnumTV.setText(carnoStrs.get(pos));
                break;
            default:
                break;
        }
    }


    //根据用户id查询用户绑定的车辆信息
    private void checkBindingCarInfo() {
        processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        EntityBean bean = new EntityBean();
        EntityBean userInfo = mApplication.getLoginInfo().getBean("userinfo");
        bean.set("uid", userInfo.get("id"));
        bean.set("token", mApplication.getToken());

        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.KCKPSEARCHCARS, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        Log.i("kckpSearchCars", "onSuccess--->" + result.toString());
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (null == view) {
                            return;
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            DrivingLicenceBeans = bean.getBeans("data");
                            if (null != DrivingLicenceBeans && DrivingLicenceBeans.length > 0) {
                                carnoList.clear();
                                for (EntityBean eBean : DrivingLicenceBeans) {
                                    carnoList.add(eBean
                                            .getString("carno"));
                                }
                                //OCR识别对方车辆的相关信息
                                setOtherCarInfo(DrivingLicenceBeans[0]);
                            }
                        }
//                        else {
//                            UiUtil.showToast(context, redes);
//                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });
    }

    //设置对方车辆的信息
    private void setOtherCarInfo(EntityBean otherBean) {
        try {
            if (otherBean == null) {
                return;
            }
            //车型
            String otype = otherBean.getString("ocartype");
            if (!TextUtils.isEmpty(otype)) {
                if (otype.startsWith("小型")) {
                    rb_accident_1.setChecked(true);
                } else if (otype.startsWith("大型")) {
                    rb_accident_2.setChecked(true);
                }
            }
            //姓名
            String oname = otherBean.getString("oname");
            if (CommontUtils.isChinaName(oname)) {
                et_accidentloadingpic1.setText(oname);
            }
            //车牌
            String ocarno = otherBean.getString("ocarno");
            if (StringUtils.isCar(ocarno)) {
                int pos = carnoStrs.indexOf(ocarno.substring(0, 1));//截取表示区域的首汉字
                pos = (pos == -1) ? 0 : pos;
                carnumTV.setText(carnoStrs.get(pos));

                String end = ocarno.substring(1);
                et_accidentloadingpic2.setText(end);
            }
            //驾驶证号
            String ocardno = otherBean.getString("ocardno");
            if (CheckCard.IDCardValidate(ocardno)) {
                et_accdentidcardhe.setText(ocardno);
            }
            //车架号
            String ovin = otherBean.getString("ovin");
            if (CommontUtils.isCarvin(ovin)) {
                framnoET.setText(ovin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object onLSMsg(int arg0, Object... arg1) {
        if (arg0 == 99) {
            //绑定成功调取查询车辆
            checkBindingCarInfo();
        }
        return null;
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            OnDestroy();
            closeForm();
        }
        return null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        DrivingLicenceBeans = null;
        if (null != carnoList) {
            carnoList.clear();
            carnoList = null;
        }
        if (null != processDialog) {
            processDialog.dismiss();
            processDialog = null;
        }
        if (null != cpsDialog) {
            cpsDialog.dismiss();
            cpsDialog = null;
        }
        regEvent(false);
    }
}
